package com.probiz.estore.customer.service.impl;

import javax.servlet.http.HttpServletRequest;

import com.probiz.estore.Constants;
import com.probiz.estore.common.model.customer.Consult;
import com.probiz.estore.common.model.customer.ConsultAndVoteVO;
import com.probiz.estore.common.model.customer.ConsultVote;
import com.probiz.estore.core.service.impl.GenericManagerImpl;
import com.probiz.estore.customer.dao.ConsultVoteDao;
import com.probiz.estore.customer.service.ConsultManager;
import com.probiz.estore.customer.service.ConsultVoteManager;
import com.probiz.estore.webapp.util.RequestContext;


/**
 * Manager implementation for ConsultVote, responsible for business processing, and communicate between web and persistence layer.
 */
public class ConsultVoteManagerImpl extends GenericManagerImpl<ConsultVote> implements ConsultVoteManager {

	private ConsultVoteDao consultVoteDao = null;
	private ConsultManager consultManager = null;

	/**
	 * @param consultVoteDao
	 *            the consultVoteDao to set
	 */
	public void setConsultVoteDao(ConsultVoteDao consultVoteDao) {
		this.consultVoteDao = consultVoteDao;
	}

	public void setConsultManager(ConsultManager consultManager) {
		this.consultManager = consultManager;
	}


	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#init()
	 */
	@Override
	protected void initManager() {
		dao = consultVoteDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onDelete(java.lang.Object)
	 */
	@Override
	protected void preDelete(ConsultVote entity) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.probiz.estore.core.service.impl.GenericManagerImpl#onSave(java.lang.Object)
	 */
	@Override
	protected void preSave(ConsultVote entity) {

	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ConsultVoteManager#isVoteAlready(java.lang.Integer, java.lang.Integer)
	 */
	public boolean isVoteAlready(Integer consultId, Integer customerId){
		return consultVoteDao.isVoteAlready(consultId, customerId);
	}
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.customer.service.ConsultVoteManager#doVote(javax.servlet.http.HttpServletRequest, java.lang.Integer, java.lang.Short)
	 */
	public ConsultAndVoteVO doVote(HttpServletRequest request,Integer consultId, Short chosen){
		ConsultAndVoteVO consultAndVoteVO = new ConsultAndVoteVO();
		Consult consult=consultManager.getById(consultId);
		//检查用户是否已经登录了
		if(RequestContext.isAnonymousUser()){
			consultAndVoteVO.setResult(ConsultAndVoteVO.VOTE_RESULT_NOTLOGIN);
			consultAndVoteVO.setConsult(consult);
			return consultAndVoteVO;
		}
		
		//检查用户是否对该咨询已经进行了投票
		if(this.isVoteAlready(consultId, RequestContext.getCurrentUserId())){
			consultAndVoteVO.setResult(ConsultAndVoteVO.VOTE_RESULT_VOTEALREADY);
			consultAndVoteVO.setConsult(consult);
			return consultAndVoteVO;
		}else{
			consultAndVoteVO.setResult(ConsultAndVoteVO.VOTE_RESULT_SUCCESS);
		}
		if (chosen.equals(Constants.FLAG_TRUE)) {
			consult.setSatisfiedCount(consult.getSatisfiedCount()+1);
		}else{
			consult.setUnsatisfiedCount(consult.getUnsatisfiedCount()+1);
		}
		ConsultVote consultVote=new ConsultVote();
		consultVote.setCustomerId(RequestContext.getCurrentUserId());
		consultVote.setConsult(consult);
		consultVote.setChosen(chosen);
		this.save(consultVote);
		consultAndVoteVO.setConsult(consult);
		consultAndVoteVO.setConsultVote(consultVote);
		return consultAndVoteVO;
	}

}
